{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dressmaker - Samples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The data model of the tables.\n",
    "\n",
    "![](../src/img/dressmaker_str.svg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " ·········\n"
     ]
    }
   ],
   "source": [
    "# Prerequesites\n",
    "import getpass\n",
    "%load_ext sql\n",
    "pwd = getpass.getpass()\n",
    "# %sql mysql+pymysql://root:$pwd@localhost:3306/sqlzoo\n",
    "%sql postgresql://postgres:$pwd@localhost/sqlzoo\n",
    "%config SqlMagic.displaylimit = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.\n",
    "The \"central\" table in this database is order_line - every garment ordered takes one line in this table. Many of the fields in this table are references to other tables. The fields of this table have the following meaning: \n",
    "\n",
    "- order_ref\n",
    "\n",
    "This is a link to the dress_order table. We can join the dress_order table to find information such as the the date of the order and the customer number for a given garment order.\n",
    "\n",
    "- line_no\n",
    "\n",
    "The line number is used to distinguish different items on the same order - for example order number 5 has three lines - 1, 2 and 3.\n",
    "\n",
    "- ol_style\n",
    "\n",
    "Indicates the article of clothing ordered. For example ol_style 1 indicates trousers - we can see this by joining to the garments table. Line 1 in the garment table is trousers.\n",
    "\n",
    "- ol_size\n",
    "\n",
    "The size of the item ordered is given here - this is particularly important when it comes to working out how much material is required to build the item. We can see from the quantities table that trousers (style 1) in size 8 takes 2.7 meters - whereas trousers in size 12 needs 2.8 meters.\n",
    "\n",
    "- ol_material\n",
    "\n",
    "Each order specifies the material to be used. We can join to the material table to find a description or cost per meter. Material 1 is Silk, Black, Plain costing £7 per meter.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "(psycopg2.ProgrammingError) can't execute an empty query\n",
      "[SQL: -- not available in postgresql]\n",
      "(Background on this error at: http://sqlalche.me/e/f405)\n"
     ]
    }
   ],
   "source": [
    "%%sql\n",
    "-- not available in postgresql"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.\n",
    "A sample join:\n",
    "\n",
    "In order to translate the numbers in order_line into meaningful values we need to join a related table. For example if we want to access the descriptions of the materials we need to join the material table.\n",
    "\n",
    "To achieve the join we include the table material on the FROM line and the join condition as a WHERE clause.\n",
    "\n",
    "For each pair of tables there is a join condition between them (if they are linked). To find the join condition between order_line and material we look at the order_line table CREATE statement and notice the line that specifies that ol_material references the material table. This link will always be to the primary key of material table.\n",
    "\n",
    "```sql\n",
    "CREATE TABLE order_line (\n",
    "  order_ref\tINTEGER\tNOT NULL REFERENCES dress_order\n",
    " ,line_no\tINTEGER\tNOT NULL\n",
    " ,ol_style\tINTEGER\tREFERENCES garment\n",
    " ,ol_size\tINTEGER\tNOT NULL\n",
    " ,ol_material\tINTEGER\tREFERENCES material\n",
    " ,PRIMARY KEY (order_ref, line_no)\n",
    " ,FOREIGN KEY (ol_style, ol_size) REFERENCES quantities\n",
    " );\n",
    "SELECT order_ref, line_no, fabric, colour, pattern, cost\n",
    "  FROM order_line, material\n",
    " WHERE ol_material = material_no\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "31 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>order_ref</th>\n",
       "        <th>line_no</th>\n",
       "        <th>fabric</th>\n",
       "        <th>colour</th>\n",
       "        <th>pattern</th>\n",
       "        <th>cost</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>1</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Black               </td>\n",
       "        <td>Plain               </td>\n",
       "        <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>2</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Red Abstract        </td>\n",
       "        <td>Printed             </td>\n",
       "        <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>1</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Yellow Stripe       </td>\n",
       "        <td>Woven               </td>\n",
       "        <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>2</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Green Stripe        </td>\n",
       "        <td>Woven               </td>\n",
       "        <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>3</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Black Dotted        </td>\n",
       "        <td>Woven               </td>\n",
       "        <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3</td>\n",
       "        <td>1</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Red Stripe          </td>\n",
       "        <td>Woven               </td>\n",
       "        <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>1</td>\n",
       "        <td>Polyester           </td>\n",
       "        <td>Pale Yellow         </td>\n",
       "        <td>Printed             </td>\n",
       "        <td>2.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>2</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Green Abstract      </td>\n",
       "        <td>Printed             </td>\n",
       "        <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>1</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Pink Check          </td>\n",
       "        <td>Woven               </td>\n",
       "        <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>2</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Green Abstract      </td>\n",
       "        <td>Printed             </td>\n",
       "        <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>3</td>\n",
       "        <td>Rayon               </td>\n",
       "        <td>Red/Orange          </td>\n",
       "        <td>Printed             </td>\n",
       "        <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>1</td>\n",
       "        <td>Serge               </td>\n",
       "        <td>Navy Blue           </td>\n",
       "        <td>Woven               </td>\n",
       "        <td>5.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>2</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Blue Abstract       </td>\n",
       "        <td>Printed             </td>\n",
       "        <td>3.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>3</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Green Abstract      </td>\n",
       "        <td>Printed             </td>\n",
       "        <td>3.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>1</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Black               </td>\n",
       "        <td>Plain               </td>\n",
       "        <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>2</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Red Abstract        </td>\n",
       "        <td>Printed             </td>\n",
       "        <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>3</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Yellow Stripe       </td>\n",
       "        <td>Woven               </td>\n",
       "        <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>1</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Green Stripe        </td>\n",
       "        <td>Woven               </td>\n",
       "        <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>2</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Black Dotted        </td>\n",
       "        <td>Woven               </td>\n",
       "        <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>3</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Red Stripe          </td>\n",
       "        <td>Woven               </td>\n",
       "        <td>3.0</td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">31 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[(1, 1, 'Silk                ', 'Black               ', 'Plain               ', 7.0),\n",
       " (1, 2, 'Silk                ', 'Red Abstract        ', 'Printed             ', 10.0),\n",
       " (2, 1, 'Cotton              ', 'Yellow Stripe       ', 'Woven               ', 3.0),\n",
       " (2, 2, 'Cotton              ', 'Green Stripe        ', 'Woven               ', 3.0),\n",
       " (2, 3, 'Cotton              ', 'Black Dotted        ', 'Woven               ', 3.0),\n",
       " (3, 1, 'Cotton              ', 'Red Stripe          ', 'Woven               ', 3.0),\n",
       " (4, 1, 'Polyester           ', 'Pale Yellow         ', 'Printed             ', 2.55),\n",
       " (4, 2, 'Silk                ', 'Green Abstract      ', 'Printed             ', 15.0),\n",
       " (5, 1, 'Cotton              ', 'Pink Check          ', 'Woven               ', 3.0),\n",
       " (5, 2, 'Silk                ', 'Green Abstract      ', 'Printed             ', 15.0),\n",
       " (5, 3, 'Rayon               ', 'Red/Orange          ', 'Printed             ', 4.0),\n",
       " (6, 1, 'Serge               ', 'Navy Blue           ', 'Woven               ', 5.5),\n",
       " (6, 2, 'Cotton              ', 'Blue Abstract       ', 'Printed             ', 3.5),\n",
       " (6, 3, 'Cotton              ', 'Green Abstract      ', 'Printed             ', 3.5),\n",
       " (7, 1, 'Silk                ', 'Black               ', 'Plain               ', 7.0),\n",
       " (7, 2, 'Silk                ', 'Red Abstract        ', 'Printed             ', 10.0),\n",
       " (7, 3, 'Cotton              ', 'Yellow Stripe       ', 'Woven               ', 3.0),\n",
       " (8, 1, 'Cotton              ', 'Green Stripe        ', 'Woven               ', 3.0),\n",
       " (8, 2, 'Cotton              ', 'Black Dotted        ', 'Woven               ', 3.0),\n",
       " (8, 3, 'Cotton              ', 'Red Stripe          ', 'Woven               ', 3.0),\n",
       " (9, 1, 'Polyester           ', 'Pale Yellow         ', 'Printed             ', 2.55),\n",
       " (10, 1, 'Cotton              ', 'Blue Stripe         ', 'Woven               ', 3.0),\n",
       " (10, 2, 'Cotton              ', 'Pink Check          ', 'Woven               ', 3.0),\n",
       " (11, 1, 'Silk                ', 'Green Abstract      ', 'Printed             ', 15.0),\n",
       " (11, 2, 'Rayon               ', 'Red/Orange          ', 'Printed             ', 4.0),\n",
       " (11, 3, 'Serge               ', 'Navy Blue           ', 'Woven               ', 5.5),\n",
       " (12, 1, 'Cotton              ', 'Blue Abstract       ', 'Printed             ', 3.5),\n",
       " (12, 2, 'Cotton              ', 'Green Abstract      ', 'Printed             ', 3.5),\n",
       " (12, 3, 'Silk                ', 'Black               ', 'Plain               ', 7.0),\n",
       " (12, 4, 'Silk                ', 'Red Abstract        ', 'Printed             ', 10.0),\n",
       " (12, 5, 'Cotton              ', 'Yellow Stripe       ', 'Woven               ', 3.0)]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT order_ref, line_no, fabric, colour, pattern, cost\n",
    "  FROM order_line, material\n",
    "    WHERE ol_material = material_no;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.\n",
    "To get a description of the garment we need to join the garment table. The join condition is that the ol_style in order_line matches the style_no in garment.\n",
    "\n",
    "```sql\n",
    "SELECT order_ref, line_no, description\n",
    "  FROM order_line, garment\n",
    " WHERE ol_style = style_no\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "31 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>order_ref</th>\n",
       "        <th>line_no</th>\n",
       "        <th>description</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>1</td>\n",
       "        <td>Trousers            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>2</td>\n",
       "        <td>Long Skirt          </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>1</td>\n",
       "        <td>Shorts              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>2</td>\n",
       "        <td>Short Skirt         </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>3</td>\n",
       "        <td>Sundress            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3</td>\n",
       "        <td>1</td>\n",
       "        <td>Suntop              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>1</td>\n",
       "        <td>Trousers            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>2</td>\n",
       "        <td>Long Skirt          </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>1</td>\n",
       "        <td>Shorts              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>2</td>\n",
       "        <td>Short Skirt         </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>3</td>\n",
       "        <td>Sundress            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>1</td>\n",
       "        <td>Trousers            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>2</td>\n",
       "        <td>Long Skirt          </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>3</td>\n",
       "        <td>Shorts              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>1</td>\n",
       "        <td>Short Skirt         </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>2</td>\n",
       "        <td>Sundress            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>3</td>\n",
       "        <td>Suntop              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>1</td>\n",
       "        <td>Suntop              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>2</td>\n",
       "        <td>Sundress            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>3</td>\n",
       "        <td>Short Skirt         </td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">31 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[(1, 1, 'Trousers            '),\n",
       " (1, 2, 'Long Skirt          '),\n",
       " (2, 1, 'Shorts              '),\n",
       " (2, 2, 'Short Skirt         '),\n",
       " (2, 3, 'Sundress            '),\n",
       " (3, 1, 'Suntop              '),\n",
       " (4, 1, 'Trousers            '),\n",
       " (4, 2, 'Long Skirt          '),\n",
       " (5, 1, 'Shorts              '),\n",
       " (5, 2, 'Short Skirt         '),\n",
       " (5, 3, 'Sundress            '),\n",
       " (6, 1, 'Trousers            '),\n",
       " (6, 2, 'Long Skirt          '),\n",
       " (6, 3, 'Shorts              '),\n",
       " (7, 1, 'Short Skirt         '),\n",
       " (7, 2, 'Sundress            '),\n",
       " (7, 3, 'Suntop              '),\n",
       " (8, 1, 'Suntop              '),\n",
       " (8, 2, 'Sundress            '),\n",
       " (8, 3, 'Short Skirt         '),\n",
       " (9, 1, 'Shorts              '),\n",
       " (10, 1, 'Long Skirt          '),\n",
       " (10, 2, 'Trousers            '),\n",
       " (11, 1, 'Trousers            '),\n",
       " (11, 2, 'Long Skirt          '),\n",
       " (11, 3, 'Shorts              '),\n",
       " (12, 1, 'Short Skirt         '),\n",
       " (12, 2, 'Sundress            '),\n",
       " (12, 3, 'Suntop              '),\n",
       " (12, 4, 'Trousers            '),\n",
       " (12, 5, 'Long Skirt          ')]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT order_ref, line_no, description\n",
    "  FROM order_line, garment\n",
    "    WHERE ol_style = style_no;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.\n",
    "If we need both the description and the fabric we can join both material and garment to the order_line table. The join conditions are combined with \"AND\"\n",
    "\n",
    "```sql\n",
    "SELECT order_ref, line_no, fabric, description\n",
    "  FROM order_line, material, garment\n",
    " WHERE ol_style = style_no\n",
    "   AND ol_material = material_no\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "31 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>order_ref</th>\n",
       "        <th>line_no</th>\n",
       "        <th>fabric</th>\n",
       "        <th>description</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>1</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Trousers            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>2</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Long Skirt          </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>1</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Shorts              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>2</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Short Skirt         </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>3</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Sundress            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3</td>\n",
       "        <td>1</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Suntop              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>1</td>\n",
       "        <td>Polyester           </td>\n",
       "        <td>Trousers            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>2</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Long Skirt          </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>1</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Shorts              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>2</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Short Skirt         </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>3</td>\n",
       "        <td>Rayon               </td>\n",
       "        <td>Sundress            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>1</td>\n",
       "        <td>Serge               </td>\n",
       "        <td>Trousers            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>2</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Long Skirt          </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>3</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Shorts              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>1</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Short Skirt         </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>2</td>\n",
       "        <td>Silk                </td>\n",
       "        <td>Sundress            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>3</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Suntop              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>1</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Suntop              </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>2</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Sundress            </td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>3</td>\n",
       "        <td>Cotton              </td>\n",
       "        <td>Short Skirt         </td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">31 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[(1, 1, 'Silk                ', 'Trousers            '),\n",
       " (1, 2, 'Silk                ', 'Long Skirt          '),\n",
       " (2, 1, 'Cotton              ', 'Shorts              '),\n",
       " (2, 2, 'Cotton              ', 'Short Skirt         '),\n",
       " (2, 3, 'Cotton              ', 'Sundress            '),\n",
       " (3, 1, 'Cotton              ', 'Suntop              '),\n",
       " (4, 1, 'Polyester           ', 'Trousers            '),\n",
       " (4, 2, 'Silk                ', 'Long Skirt          '),\n",
       " (5, 1, 'Cotton              ', 'Shorts              '),\n",
       " (5, 2, 'Silk                ', 'Short Skirt         '),\n",
       " (5, 3, 'Rayon               ', 'Sundress            '),\n",
       " (6, 1, 'Serge               ', 'Trousers            '),\n",
       " (6, 2, 'Cotton              ', 'Long Skirt          '),\n",
       " (6, 3, 'Cotton              ', 'Shorts              '),\n",
       " (7, 1, 'Silk                ', 'Short Skirt         '),\n",
       " (7, 2, 'Silk                ', 'Sundress            '),\n",
       " (7, 3, 'Cotton              ', 'Suntop              '),\n",
       " (8, 1, 'Cotton              ', 'Suntop              '),\n",
       " (8, 2, 'Cotton              ', 'Sundress            '),\n",
       " (8, 3, 'Cotton              ', 'Short Skirt         '),\n",
       " (9, 1, 'Polyester           ', 'Shorts              '),\n",
       " (10, 1, 'Cotton              ', 'Long Skirt          '),\n",
       " (10, 2, 'Cotton              ', 'Trousers            '),\n",
       " (11, 1, 'Silk                ', 'Trousers            '),\n",
       " (11, 2, 'Rayon               ', 'Long Skirt          '),\n",
       " (11, 3, 'Serge               ', 'Shorts              '),\n",
       " (12, 1, 'Cotton              ', 'Short Skirt         '),\n",
       " (12, 2, 'Cotton              ', 'Sundress            '),\n",
       " (12, 3, 'Silk                ', 'Suntop              '),\n",
       " (12, 4, 'Silk                ', 'Trousers            '),\n",
       " (12, 5, 'Cotton              ', 'Long Skirt          ')]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT order_ref, line_no, fabric, description\n",
    "  FROM order_line, material, garment\n",
    "    WHERE ol_style = style_no\n",
    "    AND ol_material = material_no;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.\n",
    "The quantities table tells us how much material is required for every garment for every size available. The join between the order_line and quantities is unusual in that it involves two fields. This can be seen by the fact that quantities has a composite key.\n",
    "\n",
    "```sql\n",
    "SELECT order_ref, line_no, quantity\n",
    "  FROM order_line, quantities\n",
    " WHERE ol_style = style_q\n",
    "   AND ol_size  = size_q\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "31 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>order_ref</th>\n",
       "        <th>line_no</th>\n",
       "        <th>quantity</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>1</td>\n",
       "        <td>2.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>2</td>\n",
       "        <td>3.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>1</td>\n",
       "        <td>1.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>2</td>\n",
       "        <td>1.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>3</td>\n",
       "        <td>3.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3</td>\n",
       "        <td>1</td>\n",
       "        <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>1</td>\n",
       "        <td>2.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>2</td>\n",
       "        <td>3.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>1</td>\n",
       "        <td>1.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>2</td>\n",
       "        <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>3</td>\n",
       "        <td>5.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>1</td>\n",
       "        <td>2.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>2</td>\n",
       "        <td>3.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>6</td>\n",
       "        <td>3</td>\n",
       "        <td>1.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>1</td>\n",
       "        <td>1.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>2</td>\n",
       "        <td>3.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>7</td>\n",
       "        <td>3</td>\n",
       "        <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>1</td>\n",
       "        <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>2</td>\n",
       "        <td>3.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>8</td>\n",
       "        <td>3</td>\n",
       "        <td>1.2</td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">31 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[(1, 1, 2.7),\n",
       " (1, 2, 3.4),\n",
       " (2, 1, 1.3),\n",
       " (2, 2, 1.2),\n",
       " (2, 3, 3.2),\n",
       " (3, 1, 1.0),\n",
       " (4, 1, 2.8),\n",
       " (4, 2, 3.8),\n",
       " (5, 1, 1.6),\n",
       " (5, 2, 1.5),\n",
       " (5, 3, 5.2),\n",
       " (6, 1, 2.7),\n",
       " (6, 2, 3.4),\n",
       " (6, 3, 1.3),\n",
       " (7, 1, 1.2),\n",
       " (7, 2, 3.2),\n",
       " (7, 3, 1.0),\n",
       " (8, 1, 1.0),\n",
       " (8, 2, 3.2),\n",
       " (8, 3, 1.2),\n",
       " (9, 1, 1.5),\n",
       " (10, 1, 4.2),\n",
       " (10, 2, 3.0),\n",
       " (11, 1, 3.0),\n",
       " (11, 2, 4.5),\n",
       " (11, 3, 1.8),\n",
       " (12, 1, 1.2),\n",
       " (12, 2, 3.2),\n",
       " (12, 3, 1.0),\n",
       " (12, 4, 2.7),\n",
       " (12, 5, 3.4)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT order_ref, line_no, quantity\n",
    "  FROM order_line, quantities\n",
    "    WHERE ol_style = style_q\n",
    "    AND ol_size  = size_q;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.\n",
    "Customers place orders - each order contains many lines - each line of the order refers to a garment:\n",
    "\n",
    "```sql\n",
    "SELECT c_name, order_date, order_no, line_no\n",
    "   FROM jmcust, dress_order, order_line\n",
    "  WHERE          jmcust.c_no = dress_order.cust_no\n",
    "  AND   dress_order.order_no = order_line.order_ref\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "31 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>c_name</th>\n",
       "        <th>order_date</th>\n",
       "        <th>order_no</th>\n",
       "        <th>line_no</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mrs Peacock         </td>\n",
       "        <td>2002-01-10</td>\n",
       "        <td>1</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mrs Peacock         </td>\n",
       "        <td>2002-01-10</td>\n",
       "        <td>1</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Dr Green            </td>\n",
       "        <td>2002-01-11</td>\n",
       "        <td>2</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Dr Green            </td>\n",
       "        <td>2002-01-11</td>\n",
       "        <td>2</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Dr Green            </td>\n",
       "        <td>2002-01-11</td>\n",
       "        <td>2</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms Muir             </td>\n",
       "        <td>2002-01-20</td>\n",
       "        <td>3</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mr Brass            </td>\n",
       "        <td>2002-02-02</td>\n",
       "        <td>4</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Mr Brass            </td>\n",
       "        <td>2002-02-02</td>\n",
       "        <td>4</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms White            </td>\n",
       "        <td>2002-02-03</td>\n",
       "        <td>5</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms White            </td>\n",
       "        <td>2002-02-03</td>\n",
       "        <td>5</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms White            </td>\n",
       "        <td>2002-02-03</td>\n",
       "        <td>5</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms Gray             </td>\n",
       "        <td>2002-02-20</td>\n",
       "        <td>6</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms Gray             </td>\n",
       "        <td>2002-02-20</td>\n",
       "        <td>6</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms Gray             </td>\n",
       "        <td>2002-02-20</td>\n",
       "        <td>6</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms Brown            </td>\n",
       "        <td>2002-02-21</td>\n",
       "        <td>7</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms Brown            </td>\n",
       "        <td>2002-02-21</td>\n",
       "        <td>7</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms Brown            </td>\n",
       "        <td>2002-02-21</td>\n",
       "        <td>7</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms Black            </td>\n",
       "        <td>2002-02-27</td>\n",
       "        <td>8</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms Black            </td>\n",
       "        <td>2002-02-27</td>\n",
       "        <td>8</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ms Black            </td>\n",
       "        <td>2002-02-27</td>\n",
       "        <td>8</td>\n",
       "        <td>3</td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">31 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[('Mrs Peacock         ', datetime.date(2002, 1, 10), 1, 1),\n",
       " ('Mrs Peacock         ', datetime.date(2002, 1, 10), 1, 2),\n",
       " ('Dr Green            ', datetime.date(2002, 1, 11), 2, 1),\n",
       " ('Dr Green            ', datetime.date(2002, 1, 11), 2, 2),\n",
       " ('Dr Green            ', datetime.date(2002, 1, 11), 2, 3),\n",
       " ('Ms Muir             ', datetime.date(2002, 1, 20), 3, 1),\n",
       " ('Mr Brass            ', datetime.date(2002, 2, 2), 4, 1),\n",
       " ('Mr Brass            ', datetime.date(2002, 2, 2), 4, 2),\n",
       " ('Ms White            ', datetime.date(2002, 2, 3), 5, 1),\n",
       " ('Ms White            ', datetime.date(2002, 2, 3), 5, 2),\n",
       " ('Ms White            ', datetime.date(2002, 2, 3), 5, 3),\n",
       " ('Ms Gray             ', datetime.date(2002, 2, 20), 6, 1),\n",
       " ('Ms Gray             ', datetime.date(2002, 2, 20), 6, 2),\n",
       " ('Ms Gray             ', datetime.date(2002, 2, 20), 6, 3),\n",
       " ('Ms Brown            ', datetime.date(2002, 2, 21), 7, 1),\n",
       " ('Ms Brown            ', datetime.date(2002, 2, 21), 7, 2),\n",
       " ('Ms Brown            ', datetime.date(2002, 2, 21), 7, 3),\n",
       " ('Ms Black            ', datetime.date(2002, 2, 27), 8, 1),\n",
       " ('Ms Black            ', datetime.date(2002, 2, 27), 8, 2),\n",
       " ('Ms Black            ', datetime.date(2002, 2, 27), 8, 3),\n",
       " ('Ms Brown            ', datetime.date(2002, 2, 27), 9, 1),\n",
       " ('Ms Gray             ', datetime.date(2002, 2, 28), 10, 1),\n",
       " ('Ms Gray             ', datetime.date(2002, 2, 28), 10, 2),\n",
       " ('Ms White            ', datetime.date(2002, 3, 1), 11, 1),\n",
       " ('Ms White            ', datetime.date(2002, 3, 1), 11, 2),\n",
       " ('Ms White            ', datetime.date(2002, 3, 1), 11, 3),\n",
       " ('Mr Brass            ', datetime.date(2002, 3, 3), 12, 1),\n",
       " ('Mr Brass            ', datetime.date(2002, 3, 3), 12, 2),\n",
       " ('Mr Brass            ', datetime.date(2002, 3, 3), 12, 3),\n",
       " ('Mr Brass            ', datetime.date(2002, 3, 3), 12, 4),\n",
       " ('Mr Brass            ', datetime.date(2002, 3, 3), 12, 5)]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT c_name, order_date, order_no, line_no\n",
    "   FROM jmcust, dress_order, order_line\n",
    "     WHERE          jmcust.c_no = dress_order.cust_no\n",
    "        AND   dress_order.order_no = order_line.order_ref;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.\n",
    "There's also a dress maker table, and a table called construction which gives you information about who made which order and when:\n",
    "\n",
    "```sql\n",
    "SELECT d_no, d_name, construction.order_ref, construction.line_ref, start_date, finish_date\n",
    " FROM dressmaker, order_line, construction\n",
    " WHERE  d_no=maker  \n",
    " AND order_line.order_ref=construction.order_ref \n",
    " AND order_line.line_no=construction.line_ref\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "30 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>d_no</th>\n",
       "        <th>d_name</th>\n",
       "        <th>order_ref</th>\n",
       "        <th>line_ref</th>\n",
       "        <th>start_date</th>\n",
       "        <th>finish_date</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>Mrs Hem             </td>\n",
       "        <td>1</td>\n",
       "        <td>1</td>\n",
       "        <td>2002-01-10</td>\n",
       "        <td>2002-03-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>Mrs Hem             </td>\n",
       "        <td>4</td>\n",
       "        <td>2</td>\n",
       "        <td>2002-02-02</td>\n",
       "        <td>2002-03-25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>Mrs Hem             </td>\n",
       "        <td>7</td>\n",
       "        <td>1</td>\n",
       "        <td>2002-02-21</td>\n",
       "        <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>Mrs Hem             </td>\n",
       "        <td>10</td>\n",
       "        <td>1</td>\n",
       "        <td>2002-02-28</td>\n",
       "        <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>1</td>\n",
       "        <td>Mrs Hem             </td>\n",
       "        <td>12</td>\n",
       "        <td>3</td>\n",
       "        <td>2002-03-03</td>\n",
       "        <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>Miss Stitch         </td>\n",
       "        <td>1</td>\n",
       "        <td>2</td>\n",
       "        <td>2002-01-10</td>\n",
       "        <td>2002-03-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>Miss Stitch         </td>\n",
       "        <td>5</td>\n",
       "        <td>1</td>\n",
       "        <td>2002-02-03</td>\n",
       "        <td>2002-03-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>Miss Stitch         </td>\n",
       "        <td>7</td>\n",
       "        <td>2</td>\n",
       "        <td>2002-02-21</td>\n",
       "        <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>Miss Stitch         </td>\n",
       "        <td>10</td>\n",
       "        <td>2</td>\n",
       "        <td>2002-03-28</td>\n",
       "        <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>Miss Stitch         </td>\n",
       "        <td>12</td>\n",
       "        <td>4</td>\n",
       "        <td>2002-03-03</td>\n",
       "        <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>2</td>\n",
       "        <td>Miss Stitch         </td>\n",
       "        <td>12</td>\n",
       "        <td>5</td>\n",
       "        <td>2002-03-03</td>\n",
       "        <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3</td>\n",
       "        <td>Mr Needles          </td>\n",
       "        <td>2</td>\n",
       "        <td>1</td>\n",
       "        <td>2002-01-11</td>\n",
       "        <td>2002-03-05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3</td>\n",
       "        <td>Mr Needles          </td>\n",
       "        <td>5</td>\n",
       "        <td>2</td>\n",
       "        <td>2002-02-03</td>\n",
       "        <td>2002-03-25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3</td>\n",
       "        <td>Mr Needles          </td>\n",
       "        <td>7</td>\n",
       "        <td>3</td>\n",
       "        <td>2002-02-21</td>\n",
       "        <td>2002-03-28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>3</td>\n",
       "        <td>Mr Needles          </td>\n",
       "        <td>11</td>\n",
       "        <td>1</td>\n",
       "        <td>2002-03-01</td>\n",
       "        <td>2002-03-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>Ms Sew              </td>\n",
       "        <td>2</td>\n",
       "        <td>2</td>\n",
       "        <td>2002-01-11</td>\n",
       "        <td>2002-03-25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>Ms Sew              </td>\n",
       "        <td>5</td>\n",
       "        <td>3</td>\n",
       "        <td>2002-02-03</td>\n",
       "        <td>2002-03-27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>Ms Sew              </td>\n",
       "        <td>8</td>\n",
       "        <td>1</td>\n",
       "        <td>2002-02-27</td>\n",
       "        <td>2002-03-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>Ms Sew              </td>\n",
       "        <td>11</td>\n",
       "        <td>2</td>\n",
       "        <td>2002-03-01</td>\n",
       "        <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>5</td>\n",
       "        <td>Mr Seam             </td>\n",
       "        <td>2</td>\n",
       "        <td>3</td>\n",
       "        <td>2002-01-11</td>\n",
       "        <td>2002-03-05</td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">30 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[(1, 'Mrs Hem             ', 1, 1, datetime.date(2002, 1, 10), datetime.date(2002, 3, 5)),\n",
       " (1, 'Mrs Hem             ', 4, 2, datetime.date(2002, 2, 2), datetime.date(2002, 3, 25)),\n",
       " (1, 'Mrs Hem             ', 7, 1, datetime.date(2002, 2, 21), None),\n",
       " (1, 'Mrs Hem             ', 10, 1, datetime.date(2002, 2, 28), None),\n",
       " (1, 'Mrs Hem             ', 12, 3, datetime.date(2002, 3, 3), None),\n",
       " (2, 'Miss Stitch         ', 1, 2, datetime.date(2002, 1, 10), datetime.date(2002, 3, 15)),\n",
       " (2, 'Miss Stitch         ', 5, 1, datetime.date(2002, 2, 3), datetime.date(2002, 3, 15)),\n",
       " (2, 'Miss Stitch         ', 7, 2, datetime.date(2002, 2, 21), None),\n",
       " (2, 'Miss Stitch         ', 10, 2, datetime.date(2002, 3, 28), None),\n",
       " (2, 'Miss Stitch         ', 12, 4, datetime.date(2002, 3, 3), None),\n",
       " (2, 'Miss Stitch         ', 12, 5, datetime.date(2002, 3, 3), None),\n",
       " (3, 'Mr Needles          ', 2, 1, datetime.date(2002, 1, 11), datetime.date(2002, 3, 5)),\n",
       " (3, 'Mr Needles          ', 5, 2, datetime.date(2002, 2, 3), datetime.date(2002, 3, 25)),\n",
       " (3, 'Mr Needles          ', 7, 3, datetime.date(2002, 2, 21), datetime.date(2002, 3, 28)),\n",
       " (3, 'Mr Needles          ', 11, 1, datetime.date(2002, 3, 1), datetime.date(2002, 3, 4)),\n",
       " (4, 'Ms Sew              ', 2, 2, datetime.date(2002, 1, 11), datetime.date(2002, 3, 25)),\n",
       " (4, 'Ms Sew              ', 5, 3, datetime.date(2002, 2, 3), datetime.date(2002, 3, 27)),\n",
       " (4, 'Ms Sew              ', 8, 1, datetime.date(2002, 2, 27), datetime.date(2002, 3, 3)),\n",
       " (4, 'Ms Sew              ', 11, 2, datetime.date(2002, 3, 1), None),\n",
       " (5, 'Mr Seam             ', 2, 3, datetime.date(2002, 1, 11), datetime.date(2002, 3, 5)),\n",
       " (5, 'Mr Seam             ', 6, 1, datetime.date(2002, 2, 20), None),\n",
       " (5, 'Mr Seam             ', 8, 2, datetime.date(2002, 2, 27), None),\n",
       " (5, 'Mr Seam             ', 11, 3, datetime.date(2002, 3, 1), None),\n",
       " (6, 'Mr Taylor           ', 3, 1, datetime.date(2002, 1, 20), datetime.date(2002, 3, 25)),\n",
       " (6, 'Mr Taylor           ', 6, 2, datetime.date(2002, 2, 20), datetime.date(2002, 3, 28)),\n",
       " (6, 'Mr Taylor           ', 8, 3, datetime.date(2002, 2, 27), None),\n",
       " (7, 'Miss Pins           ', 4, 1, datetime.date(2002, 2, 2), datetime.date(2002, 3, 5)),\n",
       " (7, 'Miss Pins           ', 6, 3, datetime.date(2002, 2, 20), None),\n",
       " (7, 'Miss Pins           ', 9, 1, datetime.date(2002, 2, 27), None),\n",
       " (7, 'Miss Pins           ', 12, 2, datetime.date(2002, 3, 3), None)]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT d_no, d_name, construction.order_ref, construction.line_ref, start_date, finish_date\n",
    " FROM dressmaker, order_line, construction\n",
    "    WHERE  d_no=maker  \n",
    "    AND order_line.order_ref=construction.order_ref \n",
    "    AND order_line.line_no=construction.line_ref;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
